package com.msconfig.mvp.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.geo.msconfig.R;
import com.msconfig.bean.dialogBean.EllipsoidParameterBean;
import com.msconfig.event.ClickEllipsoidParameterDeterminerEvent;
import com.msconfig.event.EllipsoidDialogDismissEvent;
import com.msconfig.mvp.view.activity.SetupEllipsoidSelectDialogActivity;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SetupEllipsoidParameterFragment extends BaseMvpFragment implements View.OnClickListener {
    private static SetupEllipsoidParameterFragment fragment;
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private TextView tvEllipsoidName;
    private TextView tvEllipsoidLongHalfshaft;
    private TextView tvEllipsoidFlatteningInverse;
    private RelativeLayout rlEllipsoidName;
    private Button btnDetermine;
    private EllipsoidParameterBean ellipsoidBean;
    private static final String ELLIPSOIDNAME="ellipsoidName";
    private static final String ELLIPSOIDLONGHALFSHAFT="ellipsoidLongHalfshaft";
    private static final String ELLIPSOIDFLATTENINGINVERSE="ellipsoidFlatteningInverse";
    public static final String RLELLIPSOIDNAME="RLELLIPSOIDNAME";

    //传过来的参数
    public static SetupEllipsoidParameterFragment newInstance(EllipsoidParameterBean ellipsoidBean) {
        Bundle args = new Bundle();
        fragment = new SetupEllipsoidParameterFragment();

        if(ellipsoidBean==null){
            fragment.setArguments(null);
        }else{
            args.putString(ELLIPSOIDNAME,ellipsoidBean.getName());
            args.putString(ELLIPSOIDLONGHALFSHAFT,ellipsoidBean.getLongHalfshaft());
            args.putString(ELLIPSOIDFLATTENINGINVERSE,ellipsoidBean.getFlatteningInverse());
            fragment.setArguments(args);
        }
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_setup_ellipsoid_parameter;
    }


    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        rlEllipsoidName=view.findViewById(R.id.rl_ellipsoid_name);
        tvEllipsoidName=view.findViewById(R.id.tv_ellipsoid_name);
        tvEllipsoidLongHalfshaft=view.findViewById(R.id.tv_ellipsoid_long_halfshaft);
        tvEllipsoidFlatteningInverse=view.findViewById(R.id.tv_ellipsoid_flattening_inverse);
        btnDetermine=view.findViewById(R.id.btn_determine);
    }

    //目前这里只是为了实现eventbus
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void showSelectEllipsoidParameter(EllipsoidDialogDismissEvent event) {
        EllipsoidParameterBean bean=event.getEllipsoidBean();
        //显示对应椭球的数据
        tvEllipsoidName.setText(bean.getName());
        tvEllipsoidLongHalfshaft.setText(bean.getLongHalfshaft());
        tvEllipsoidFlatteningInverse.setText(bean.getFlatteningInverse());
    }


    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
        rlEllipsoidName.setOnClickListener(this);
        btnDetermine.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_ellipsoid_parameter);

        Bundle bundle = fragment.getArguments();
        if(bundle!=null){
            tvEllipsoidName.setText(bundle.getString(ELLIPSOIDNAME));
            tvEllipsoidLongHalfshaft.setText(bundle.getString(ELLIPSOIDLONGHALFSHAFT));
            tvEllipsoidFlatteningInverse.setText(bundle.getString(ELLIPSOIDFLATTENINGINVERSE));
        }
//        else{//显示默认的
//            tvEllipsoidName.setText("CGCS2000");
//            tvEllipsoidLongHalfshaft.setText("6378137");
//            tvEllipsoidFlatteningInverse.setText("298.257223563");
//        }

    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }else if(v==rlEllipsoidName){
            Intent intent=new Intent(getActivity(),SetupEllipsoidSelectDialogActivity.class);
            intent.putExtra(RLELLIPSOIDNAME,tvEllipsoidName.getText().toString().trim());
            startActivity(intent);
        }else if(v==btnDetermine){
            onClickDetermine();
        }
    }

    private void onClickDetermine() {
        //回调把信息返回回去
        if(tvEllipsoidName.getText().toString().trim().equals("")){
            Toast.makeText(getActivity(), R.string.toast_choose_ellipsoid_info, Toast.LENGTH_SHORT).show();
            return;
        }else{
            backTo();
            ellipsoidBean=new EllipsoidParameterBean();
            ellipsoidBean.setName(tvEllipsoidName.getText().toString().trim());
            ellipsoidBean.setLongHalfshaft(tvEllipsoidLongHalfshaft.getText().toString().trim());
            ellipsoidBean.setFlatteningInverse(tvEllipsoidFlatteningInverse.getText().toString().trim());
            EventBus.getDefault().post(new ClickEllipsoidParameterDeterminerEvent(ellipsoidBean) );
        }
    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

}
